# API 接口测试脚本
# 用于测试图书管理系统的 4 个核心接口

$baseUrl = "http://localhost:3001/api/books"

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  图书管理系统 API 接口测试" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# 测试 1: 查询所有图书 (GET)
Write-Host "【测试 1】查询所有图书 (GET /api/books)" -ForegroundColor Yellow
Write-Host "----------------------------------------"
try {
    $response = Invoke-RestMethod -Uri $baseUrl -Method Get
    Write-Host "✅ 测试通过" -ForegroundColor Green
    Write-Host "返回数据: $($response.data.books.Count) 本图书"
    Write-Host "分页信息: 第 $($response.data.pagination.currentPage) 页，共 $($response.data.pagination.totalPages) 页"
    Write-Host ""
} catch {
    Write-Host "❌ 测试失败: $_" -ForegroundColor Red
}

# 测试 2: 新增图书 (POST)
Write-Host "【测试 2】新增图书 (POST /api/books)" -ForegroundColor Yellow
Write-Host "----------------------------------------"
$newBook = @{
    title = "AI编程实战指南"
    author = "测试老师"
    publishDate = "2024-01-15"
    price = 128.50
    isbn = "9787302123456"
    publisher = "清华大学出版社"
    category = "人工智能"
    description = "深入学习AI辅助编程的实战技巧"
    stock = 50
} | ConvertTo-Json

try {
    $response = Invoke-RestMethod -Uri $baseUrl -Method Post -Body $newBook -ContentType "application/json"
    $bookId = $response.data._id
    Write-Host "✅ 测试通过" -ForegroundColor Green
    Write-Host "新增图书 ID: $bookId"
    Write-Host "书名: $($response.data.title)"
    Write-Host ""
    
    # 保存 ID 用于后续测试
    $global:testBookId = $bookId
    
} catch {
    Write-Host "❌ 测试失败: $_" -ForegroundColor Red
}

# 等待数据写入
Start-Sleep -Seconds 1

# 测试 3: 查询单本图书 (GET by ID)
Write-Host "【测试 3】查询单本图书 (GET /api/books/:id)" -ForegroundColor Yellow
Write-Host "----------------------------------------"
if ($global:testBookId) {
    try {
        $response = Invoke-RestMethod -Uri "$baseUrl/$global:testBookId" -Method Get
        Write-Host "✅ 测试通过" -ForegroundColor Green
        Write-Host "图书 ID: $($response.data._id)"
        Write-Host "书名: $($response.data.title)"
        Write-Host "作者: $($response.data.author)"
        Write-Host "价格: ¥$($response.data.price)"
        Write-Host ""
    } catch {
        Write-Host "❌ 测试失败: $_" -ForegroundColor Red
    }
} else {
    Write-Host "⚠️  跳过测试（未获取到图书 ID）" -ForegroundColor Yellow
}

# 测试 4: 删除图书 (DELETE)
Write-Host "【测试 4】删除图书 (DELETE /api/books/:id)" -ForegroundColor Yellow
Write-Host "----------------------------------------"
if ($global:testBookId) {
    try {
        $response = Invoke-RestMethod -Uri "$baseUrl/$global:testBookId" -Method Delete
        Write-Host "✅ 测试通过" -ForegroundColor Green
        Write-Host "已删除图书: $($response.data.title)"
        Write-Host "图书 ID: $($response.data.id)"
        Write-Host ""
    } catch {
        Write-Host "❌ 测试失败: $_" -ForegroundColor Red
    }
} else {
    Write-Host "⚠️  跳过测试（未获取到图书 ID）" -ForegroundColor Yellow
}

# 验证删除
Write-Host "【验证】再次查询所有图书" -ForegroundColor Yellow
Write-Host "----------------------------------------"
try {
    $response = Invoke-RestMethod -Uri $baseUrl -Method Get
    Write-Host "✅ 验证完成" -ForegroundColor Green
    Write-Host "当前图书总数: $($response.data.pagination.totalItems) 本"
    Write-Host ""
} catch {
    Write-Host "❌ 验证失败: $_" -ForegroundColor Red
}

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  测试完成！" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "📊 测试总结:" -ForegroundColor Green
Write-Host "   ✅ GET  /api/books      - 查询所有图书"
Write-Host "   ✅ POST /api/books      - 新增图书"
Write-Host "   ✅ GET  /api/books/:id  - 查询单本图书"
Write-Host "   ✅ DELETE /api/books/:id - 删除图书"
Write-Host ""
